#include <SN8P2711B.h>

#define TRUE		1
#define FALSE	0

#define _LIGHT_ON	0
#define _LIGHT_OFF	1

#define _LED1			(FP03)	//output
#define _LED2			(FP02)	//output
#define _LED3			(FP53)	//output
#define _LED4			(FP54)	//output
#define _LED5			(FP43)	//output
#define _OUT_EN			(FP41)	//output
#define _VER_CON		(FP00)	//output
#define _KEY_DETECT		(FP04)	//input
#define _VIN_DETECT		(FP01)	//input
//#define _LIPOW_DETECT	(FP44)	//input	AIN4

#define _20MSTIMEOUT		10		// 20 ms
#define _100MSTIMEOUT		50		// 100 ms
#define _500MSTIMEOUT		5		// 500 ms = _100MSTIMEOUT x 5
#define _1000MSTIMEOUT		10		// 1 Sec = _100MSTIMEOUT x 10
#define _1MinTIMEOUT		60		// 1 min = _1000MSTIMEOUT x 60
#define _30MinTIMEOUT		30		// 1 min = _1MinTIMEOUT x 30
#define _SLEEPTIMEOUT		25		// 30 Sec = _1000MSTIMEOUT x 25

typedef enum
{
	EN_STATE_IDLE	= 0x00,
	EN_STATE_OUTPUT,
	EN_STATE_CHARGE,
	//EN_STATE_CHARGE_OK,
	EN_STATE_LOWPWOER,
	EN_STATE_GOING2SLEEP,
	EN_STATE_SLEEP,
	//EN_STATE_TOTAL

} EN_STATE;

EN_STATE enState = EN_STATE_IDLE;	//0:ilde, 1:Li Charge,  2:Output

//bit g2MsTimeoutTick = FALSE;
bit g20MsTimeoutTick = FALSE;
bit g100MsTimeoutTick = FALSE;
//bit g500MsTimeoutTick = FALSE;
bit g1000MsTimeoutTick = FALSE;
bit gsleepTimeoutTick = FALSE;
bit g1MinTimeoutTick = FALSE;
bit g30MinTimeoutTick = FALSE;
bit gbFristPowerOn = TRUE;
unsigned char gbFristPowerOn2 = 3;
bit gbProtectionSleep = FALSE;
bit gbChargeOK = FALSE;
bit gbOutputChargeOK = FALSE;
unsigned char gbOutputChargeOKReadyToSleep = 0;
bit gbDeviceInsert = TRUE;
bit gbBootingState = TRUE;
bit gbBootCheckDev = FALSE;
bit gbBootingState3 = TRUE;
bit gucADCSel = 0;
//bit gbSleepState = FALSE;
unsigned char g5MsTimeoutCount = 0;
unsigned char g20MsTimeoutCount = 0;
unsigned char g100MsTimeoutCount = 0;
unsigned char g500MsTimeoutCount = 0;
unsigned char g1000MsTimeoutCount = 0;
unsigned char g1MinTimeoutCount = 0;
unsigned char g30MinTimeoutCount = 0;
unsigned char gsleepTimeoutCount = 0;
unsigned char glowPowerTimeoutCount = 0;
unsigned char ucPowerPercent = 0xFF;
unsigned char gsleepTimeoutTotal = _SLEEPTIMEOUT;
unsigned char gFlashLEDTimeoutCount = 0;
unsigned char gFlashLEDTimeoutTotal = 5;

unsigned char ucADCValBuf[6];	
unsigned char ucADCCount = 0;

void systemChangeState(EN_STATE enStateVal);
void showInfoLEDsByLIPower(unsigned char ucPercent);
//void AIN4ADCHandel(unsigned char ulDataH, unsigned char ulDataL);
void AIN2ADCHandel(void);
void ADC_Process(void);

void main(void)
{
	//WDTR = 0x5A;

	P0M = 0x0D;	//P0 Input-Output value
	P0UR = 0x00;	//P0 pull up value
	P0 = 0X0C;	//P0 register value
	P4M = 0x0A;	//P4 Input-Output value	0x0E
	P4UR = 0x00;     //P4 pull up value
	P4CON = 0x11;	//P4 Analog-Digital value	0x11
	P4 = 0X00;	//P4 register value	
	P5M = 0x18;	//P5 Input-Output value
	P5UR = 0x00;	//P5 pull up value
	P5 = 0X00;	//P5 register value	

	TC0M = 0x24;	//TC0M register value, Fcpu: 1.000000MHZ
	FTC0X8 = 0;	//CLK source: Fcpu
	TC0C = 0x64;	//TC0C register value, time: 10.000000ms(100.000000HZ)
	TC0R = 0x64;	//TC0R register value
	FTC0IRQ = 0;
	FTC0IEN = 1;	//TC0 Interrupt enable
	FTC0CKS = 0;
	FTC0ENB = 1;	//TC0 enable
#if 0
	ADM = 0x02;	//ADC input channel mode: AIN4
	FGCHS = 1;	//AIN channel enable
	//_DELAY(100); //User support DELAY function
	ADR = 0x00;	//ADC clock: 1.000000MHZ,ADC resolution: -1
	VREFH = 0x00;	//Internal VREFH: NULL
	FEVHENB = 1;	//Internal VREFH disable
	FADENB = 1;
	FADCIRQ = 0;
	FADCIEN = 1;	//Interrupt enable
	FEOC = 0;
	//FADS = 1;
#endif
	_LED1 = _LIGHT_OFF;
	_LED2 = _LIGHT_OFF;
	_LED3 = _LIGHT_OFF;
	_LED4 = _LIGHT_OFF;
	_LED5 = FALSE;
	_OUT_EN = FALSE;

	FGIE = 1;

	while(1)
	{
		//WDTR = 0x5A;

		if(gbFristPowerOn)
		{
			if (gbFristPowerOn2==3)
			{
				_LED1 = _LIGHT_ON;
				_LED2 = _LIGHT_OFF;
				_LED3 = _LIGHT_OFF;			
				continue;
			}

			if (gbFristPowerOn2==2)
			{
				_LED1 = _LIGHT_OFF;
				_LED2 = _LIGHT_ON;
				_LED3 = _LIGHT_OFF;			
				continue;
			}

			if (gbFristPowerOn2==1)
			{
				_LED1 = _LIGHT_OFF;
				_LED2 = _LIGHT_OFF;
				_LED3 = _LIGHT_ON;						
				continue;
			}

			if (gbFristPowerOn2==0)
			{
				_LED1 = _LIGHT_OFF;
				_LED2 = _LIGHT_OFF;
				_LED3 = _LIGHT_OFF;						
				systemChangeState(EN_STATE_SLEEP);
				gbFristPowerOn = FALSE;			
			}
						
		}

/*		
		if(g20MsTimeoutTick)
		{
			g20MsTimeoutTick = FALSE;
		}


		if(g100MsTimeoutTick)
		{
			g100MsTimeoutTick = FALSE;
			if (glowPowerTimeoutCount > 0)
				glowPowerTimeoutCount--;			

		}
*/
		if (gbBootingState)
		{
			_OUT_EN = FALSE;
			_VER_CON = 1;
			P4M = 0x0A;	//P4 Input-Output value	0x0E
			P4UR = 0x00;     //P4 pull up value
			P4CON = 0x15;	
							
			continue;
		}

		if (!gbBootCheckDev)
		{
		#if 0
			if ((!_VIN_DETECT) && (!FP42))
			{
				systemChangeState(EN_STATE_SLEEP);
				continue;
			}
			else
		#endif				
			{
				_OUT_EN = TRUE;
			
				ADM = 0x04;	//ADC input channel mode: AIN4
				FGCHS = 1;	//AIN channel enable
				//_DELAY(100); //User support DELAY function
				//ADR = 0x00;	//ADC clock: 1.000000MHZ,ADC resolution: -1
				//VREFH = 0x00;	//Internal VREFH: NULL
				FEVHENB = 1;	//Internal VREFH disable
				FADENB = 1;
				FADCIRQ = 0;
				FADCIEN = 1;	//Interrupt enable
				FEOC = 0;
				gucADCSel = 0;
			
				gbBootCheckDev = TRUE;
			}			
		}

		if (gbBootingState3)
			continue;
		
		if (_VIN_DETECT)
		{
			if (enState!=EN_STATE_CHARGE)
				systemChangeState(EN_STATE_CHARGE);
		}
		
		if (enState == EN_STATE_IDLE)		//ilde
		{
			_OUT_EN = TRUE;
			if (ucPowerPercent != 0)
			{
				if(gsleepTimeoutTick)
				{
					gsleepTimeoutTick = FALSE;

					enState = EN_STATE_GOING2SLEEP;
					gbOutputChargeOKReadyToSleep = 3;					
					//systemChangeState(EN_STATE_SLEEP);
					//_LED2 = _LIGHT_ON;
					//continue;					
				}

				if (gbDeviceInsert == 1)	//device insert
				{
					systemChangeState(EN_STATE_OUTPUT);						
				}						
			}
			else
			{
				systemChangeState(EN_STATE_LOWPWOER);
			}
		}
		
		if (enState == EN_STATE_LOWPWOER)
		{
			//LED1 flash 3 times then change to IDLE
			if (glowPowerTimeoutCount)
			{					
				_LED1 = glowPowerTimeoutCount&0x01;				
			}
			else
			{
				_LED1 = _LIGHT_OFF;
				//systemChangeState(EN_STATE_SLEEP);
				gbOutputChargeOKReadyToSleep = 3;
				enState = EN_STATE_GOING2SLEEP;				
			}				
		}
		
		if(enState == EN_STATE_CHARGE)		//Charge
		{
			if (!_VIN_DETECT)
			{
				gbBootingState3 = TRUE;
				systemChangeState(EN_STATE_OUTPUT);
			}

		/*
			if (!_VIN_DETECT)
			{
				if (gbDeviceInsert == 0)
				{
					systemChangeState(EN_STATE_SLEEP);
				}
				else
				{
					systemChangeState(EN_STATE_OUTPUT);
				}
			}
			*/
			/*
			else
			{
				if (gbDeviceInsert == 0)
				{
					_OUT_EN = FALSE;
				}
				else
				{
					_OUT_EN = TRUE;
					if(gbProtectionSleep)
					{	
						//systemChangeState(EN_STATE_SLEEP);
						gbOutputChargeOKReadyToSleep = 3;
						enState = EN_STATE_GOING2SLEEP;	
						continue;
					}
					
				}
			}
			*/
		}

		if(enState == EN_STATE_OUTPUT)		//Output
		{
			//if((gbProtectionSleep)||(gbOutputChargeOK))
			if(gbProtectionSleep)
			{	
				//systemChangeState(EN_STATE_SLEEP);
				gbOutputChargeOKReadyToSleep = 3;
				enState = EN_STATE_GOING2SLEEP;	
				continue;
			}

			if(ucPowerPercent == 0)
			{
				systemChangeState(EN_STATE_LOWPWOER);
			}
			else
			{
				//if ((gbLoadStateChange==1) && (gbLoadState == 0))	//no device insert
				if (gbDeviceInsert == 0)	//no device insert
				{
					//_LED5=1;
					//gbLoadState = 0;
					//showInfoLEDsByLIPower(ucPowerPercent, FALSE);
					systemChangeState(EN_STATE_IDLE);
					//_LED4 = _LIGHT_ON;
					gsleepTimeoutTotal = 4;	//5Sec
				}
			}
		}		

		if(enState == EN_STATE_GOING2SLEEP)		//Output OK
		{
			//if(gbOutputChargeOK)
			{
				_OUT_EN = FALSE;	
				//_LED4 = 0;	//for test
				//FGIE = 0;
				ADM = 0x00;
				FGCHS = 0;	//AIN channel enable
				//ADR = 0x00;	//ADC clock: 1.000000MHZ,ADC resolution: -1
				//VREFH = 0x00;	//Internal VREFH: NULL
				FEVHENB = 1;	//Internal VREFH disable
				FADENB = 0;
				FADCIRQ = 0;
				FADCIEN = 0;	//Interrupt enable
				FEOC = 0;		
				_VER_CON = 0;
				
				P4M = 0x0A;	//P4 Input-Output value	0x0E
				P4UR = 0x00;     //P4 pull up value
				P4CON = 0x11;	//P4 Analog-Digital value	0x11
				//_LED4 = _LIGHT_OFF;

				if(gbOutputChargeOKReadyToSleep == 0)
				{
					systemChangeState(EN_STATE_SLEEP);
				}
				continue;				
			}			
		}		
	}

}

__interrupt ISR(void)
{
	//unsigned long lldata = 0;

	if(FTC0IRQ)
	{
		FTC0IRQ=0;

		//if (g5MsTimeoutCount < 2)
		g5MsTimeoutCount++;

		if (g5MsTimeoutCount == 2)
		{
			if((enState != EN_STATE_GOING2SLEEP) && gbBootCheckDev)
				FADS = 1;
			//g5MsTimeoutCount = 0xff;
		}			
		
		g20MsTimeoutCount++;
		if (g20MsTimeoutCount >= _20MSTIMEOUT)		//20ms
		{
			g20MsTimeoutCount = 0;
			g20MsTimeoutTick = TRUE;

			if(!gbFristPowerOn)
				gbBootingState = FALSE;					
		}

		g100MsTimeoutCount++;
		if (g100MsTimeoutCount >= _100MSTIMEOUT)		//100ms
		{
			g100MsTimeoutCount = 0;
			g100MsTimeoutTick = TRUE;

			if (glowPowerTimeoutCount > 0)
				glowPowerTimeoutCount--;			

			gFlashLEDTimeoutCount++;
			if (gFlashLEDTimeoutCount>=gFlashLEDTimeoutTotal)
			{
				gFlashLEDTimeoutCount = 0;
				
				if(enState != EN_STATE_LOWPWOER)
				{
					showInfoLEDsByLIPower(ucPowerPercent);
				}				
			}

			if (gbBootCheckDev)
				gbBootingState3 = FALSE;

			if (gbOutputChargeOKReadyToSleep>0)
				gbOutputChargeOKReadyToSleep--;		
			
			g500MsTimeoutCount++;

			if (g500MsTimeoutCount >= _500MSTIMEOUT)		//500ms
			{
				g500MsTimeoutCount = 0;
				//g500MsTimeoutTick = TRUE;

				if(gbFristPowerOn2>0)				
					gbFristPowerOn2--;			
				//else
				//if(gbBootingState)
				//gbBootingState = FALSE;									
			}

			g1000MsTimeoutCount++;
			if (g1000MsTimeoutCount >= _1000MSTIMEOUT)		//1Sec
			{
				g1000MsTimeoutCount = 0;
				g1000MsTimeoutTick = TRUE;			
									
				g1MinTimeoutCount++;
				if (g1MinTimeoutCount >= _1MinTIMEOUT)		// 1Min
				{
					g1MinTimeoutCount = 0;
					g1MinTimeoutTick = TRUE;

					//if (ucPowerPercent == 99)
					if (ucPowerPercent == 9)
					{
						g30MinTimeoutCount++;
						if (g30MinTimeoutCount >= _30MinTIMEOUT)		// 30Min
						{
							g30MinTimeoutCount = 0;
							g30MinTimeoutTick = TRUE;
							gbChargeOK = TRUE;
						}
					}
					else
					{
						g30MinTimeoutCount = 0;
						g30MinTimeoutTick = FALSE;
					}
				}
				
				//if (gbFristPowerOn == TRUE)
				//	gbFristPowerOn = FALSE;
				
				if (enState == EN_STATE_IDLE)
				{
					gsleepTimeoutCount++;
					//if (++gsleepTimeoutCount >= _SLEEPTIMEOUT)		//sleeptimeout
					if (gsleepTimeoutCount >= gsleepTimeoutTotal)		//sleeptimeout
					{
						gsleepTimeoutCount = 0;
						gsleepTimeoutTick = TRUE;				
					}
				}
				else
				{
					gsleepTimeoutCount = 0;
					gsleepTimeoutTick = FALSE;				
				}
			}		
		}
	}

	if(enState != EN_STATE_GOING2SLEEP)
	{
		if((FADCIEN==1)&&(FADCIRQ==1))
		{
			if (FEOC)
			{	
				//lldata = ADB;
				//lldata = (lldata<<4) |(ADR&0x0F);
				//getADCHandel(lldata);
					
				if(gucADCSel == 0)
				{
					ADC_Process();
				}
				else
				{
					AIN2ADCHandel();
				}
			}
		}
	}	
}

void showInfoLEDsByLIPower(unsigned char ucPercent)
{
	if (gbBootingState ||!gbBootCheckDev||gbBootingState3)
		return;

	gFlashLEDTimeoutTotal = 5;
	if (ucPercent == 1)
	{
		gFlashLEDTimeoutTotal = 3;
	}
	
	if((ucPercent == 1) ||(ucPercent == 6))
	{
		_LED1 = !_LED1;
		_LED2 = _LIGHT_OFF;
		_LED3 = _LIGHT_OFF;
		//_LED4 = _LIGHT_OFF;
	}
	if(ucPercent == 2)
	{
		_LED1 = _LIGHT_ON;
		_LED2 = _LIGHT_OFF;
		_LED3 = _LIGHT_OFF;
		//_LED4 = !_LED4;
	}
	if(ucPercent == 3)
	{
		_LED1 = _LIGHT_OFF;
		_LED2 = _LIGHT_ON;
		_LED3 = _LIGHT_OFF;
		//_LED4 = !_LED4;
	}
	if(ucPercent == 4)
	{
		_LED1 = _LIGHT_OFF;
		_LED2 = _LIGHT_OFF;
		_LED3 = _LIGHT_ON;
		//_LED4 = !_LED4;
	}
	if(ucPercent == 7)
	{
		_LED1 = _LIGHT_OFF;
		_LED2 = !_LED2;
		_LED3 = _LIGHT_OFF;
		//_LED4 = _LIGHT_OFF;
	}
	if(ucPercent == 8)
	{
		_LED1 = _LIGHT_OFF;
		_LED2 = _LIGHT_OFF;
		_LED3 = !_LED3;
		//_LED4 = _LIGHT_OFF;
	}
	if(ucPercent == 9)
	{
		_LED1 = _LIGHT_OFF;
		_LED2 = _LIGHT_OFF;
		_LED3 = _LIGHT_ON;
		//_LED4 = !_LED4;
	}
}

void systemChangeState(EN_STATE enStateVal)
{
	//if(enStateVal >= EN_STATE_TOTAL)
		//return;

	enState = enStateVal;
	//gbSleepState = FALSE; 
	gsleepTimeoutTotal = 4;//_SLEEPTIMEOUT;
	
	if (enStateVal == EN_STATE_IDLE)
	{
		//_LED1 = _LIGHT_OFF;
		//_LED2 = _LIGHT_OFF;
		//_LED3 = _LIGHT_OFF;
		//_LED4 = _LIGHT_OFF;
		//_LED5 = FALSE;
		_OUT_EN = TRUE;
		//_CHARGE_ON_OFF = FALSE;
		//ucPowerPercent = 5;
	}

	if (enStateVal == EN_STATE_LOWPWOER)
	{
		_LED1 = _LIGHT_ON;
		_LED2 = _LIGHT_OFF;
		_LED3 = _LIGHT_OFF;
		//_LED4 = _LIGHT_OFF;
		//_LED5 = FALSE;
		_OUT_EN = FALSE;
		glowPowerTimeoutCount = 4;
		//ucPowerPercent = 0;
		//NOP(1);
	}	

	if (enStateVal == EN_STATE_CHARGE)
	{
		//_LED5 = FALSE;
		_OUT_EN = FALSE;
		//_CHARGE_ON_OFF = TRUE;
		ucPowerPercent = 1;
	}

	if (enStateVal == EN_STATE_OUTPUT)
	{
		//_CHARGE_ON_OFF = FALSE;
		_OUT_EN = TRUE;			
		//DELAY(100);
		ucPowerPercent = 5;
	}

	if (enStateVal == EN_STATE_SLEEP)
	{
		_LED1 = _LIGHT_OFF;
		_LED2 = _LIGHT_OFF;
		_LED3 = _LIGHT_OFF;
		//_LED4 = _LIGHT_OFF;
		//_LED5 = FALSE;
				FGIE = 0;

/*
		_OUT_EN = FALSE;	
		//_LED4 = 0;	//for test
		FGIE = 0;
		ADM = 0x00;
		FGCHS = 0;	//AIN channel enable
		ADR = 0x00;	//ADC clock: 1.000000MHZ,ADC resolution: -1
		VREFH = 0x00;	//Internal VREFH: NULL
		FEVHENB = 1;	//Internal VREFH disable
		FADENB = 0;
		FADCIRQ = 0;
		FADCIEN = 0;	//Interrupt enable
		FEOC = 0;		
		_VER_CON = 0;
		NOP(3);
		P4M = 0x00;	//P4 Input-Output value	0x0E
		P4UR = 0xFF;     //P4 pull up value
		P4CON = 0x00;	//P4 Analog-Digital value	0x11
		FP42 = 1;	//P4 register value		
		NOP(3);
*/		
		SLEEPMODE;
		NOP(3);
		
		//_LED5 = 1;	//for test
		
		//init all data
		g20MsTimeoutTick = FALSE;
		g100MsTimeoutTick = FALSE;
		//g500MsTimeoutTick = FALSE;
		g1000MsTimeoutTick = FALSE;
		gsleepTimeoutTick = FALSE;
		g20MsTimeoutCount = 0;
		g100MsTimeoutCount = 0;
		g500MsTimeoutCount = 0;
		g1000MsTimeoutCount = 0;
		gsleepTimeoutCount = 0;
		glowPowerTimeoutCount = 0;
		gFlashLEDTimeoutCount = 0;
		gFlashLEDTimeoutTotal = 3;
		//if (ucPowerPercent==0)
		//ucPowerPercent = 0xFF;
		gbDeviceInsert = TRUE;
		gbBootingState = TRUE;
		gbBootCheckDev = FALSE;
		gbBootingState3 = TRUE;
		//gbNewLoadState = 0;
		//gbLoadStateChange = 0;
		//gADCCount = 0;
		gbProtectionSleep = FALSE;
		g30MinTimeoutTick = FALSE;
		gbChargeOK = FALSE;
		ucADCCount = 0;
		gucADCSel = 0;
		g5MsTimeoutCount = 0;
		
		if (ucPowerPercent>=6)
			ucPowerPercent -= 5;
		
		//_VER_CON = 1;
#if 0
		ADM = 0x04;	//ADC input channel mode: AIN2
		FGCHS = 1;	//AIN channel enable
		//FADENB = 1;
		ADR = 0x00;	//ADC clock: 1.000000MHZ,ADC resolution: -1
		VREFH = 0x00;	//Internal VREFH: NULL
		FEVHENB = 1;	//Internal VREFH disable		
		FADENB = 1;		
		FADCIRQ = 0;
		FADCIEN = 1;	//Interrupt enable
		FEOC = 0;
#endif		
		FGIE = 1;
		enState = EN_STATE_IDLE;				
	}

	gbOutputChargeOK = FALSE;
	return;
}

								//  4.0  	  3.7	     3.35       3			2.495
__ROM unsigned char ADCTable[12] = {0xCDu, 0xBEu, 0xACu, 0x9Au, 			//idle
								//     4.15    3.90     3.60      3		2.495
									0xD5u, 0xCEu, 0xB8u, 0x9Au,		//charge
								//     3.80    3.45    3.25        3			2.495								
									0xC3u, 0xB1u, 0xA8u, 0x9Au};		//output

__ROM unsigned char ADCTable2[12] = {4, 3, 2, 1,
								     9, 8, 7, 6,
								     4, 3, 2, 1};
void AIN2ADCHandel(void)
{
	unsigned char ulDataH,  ulDataL;
	static unsigned char ucPrevDataH, ucPrevDataL;
	static unsigned char ucpowerpercentCount = 0;  // confirm powerpercent is ok

	ulDataH = ADB;
	ulDataL = ADR&0x0F;
	//if((!gbBootingState) & (ucPrevDataH==ulDataH)&(ucPrevDataL==ulDataL))
	if((ucPrevDataH==ulDataH)&(ucPrevDataL==ulDataL))
	{
		ucpowerpercentCount++;
		if(ucpowerpercentCount == 3)
		{
			ucpowerpercentCount = 0;

			if((ulDataH==0))
			{
				if(ulDataL<=0x01)
				{
					gbDeviceInsert = FALSE;
				}
				else
				{
					gbDeviceInsert = TRUE;

					if ((gbBootCheckDev)&&(enState == EN_STATE_OUTPUT))
					{
						if (ulDataL<=6)
						{
							gbOutputChargeOK = TRUE;
							gbOutputChargeOKReadyToSleep = 2;
							enState = EN_STATE_GOING2SLEEP;
							return;
						}
						else
						{
							gbOutputChargeOK = FALSE;
							gbOutputChargeOKReadyToSleep = 0;
						}
					}
				}				
			}
			else
			{
				gbDeviceInsert = TRUE;
			}

			if (ulDataH==0x12)
			{
				gbProtectionSleep = TRUE;//go sleep
			}
			if (ulDataH==0x13)
			{
				gbProtectionSleep = TRUE;//go sleep
			}
		}
	}
	else
	{
		ucPrevDataH = ulDataH;
		ucPrevDataL	= ulDataL;
		ucpowerpercentCount = 0;
		
	}

	ADC123:
	FADENB = 0;
	ADM = 0x04;	//ADC input channel mode: AIN4
	FGCHS = 1;	//AIN channel enable
	//ADR = 0x00;	//ADC clock: 1.000000MHZ,ADC resolution: -1
	//VREFH = 0x00;	//Internal VREFH: NULL
	FEVHENB = 1;	//Internal VREFH disable
	FADENB = 1;
	FADCIRQ = 0;
	FADCIEN = 1;	//Interrupt enable
	FEOC = 0;
	//FADS = 1;					
	
	gucADCSel = 0;
	g5MsTimeoutCount = 0;
}

void ADC_Process(void)
{
	unsigned char ucMax, ucMin;
	unsigned long ucADCValSum = 0;	
	
	//Init_ADC();
	//FADS = 1;
	
	//while(!FEOC);
	
	//FADS = 0;
	ucADCValBuf[ucADCCount] = ADB;
	ucADCCount++;

	if(ucADCCount>=6)
	{
		//ucADCCount = 0;
		ucMax=0, ucMin=0xff;
		for(ucADCCount=0; ucADCCount<6; ucADCCount++)
		{
			ucADCValSum+=ucADCValBuf[ucADCCount];

			if(ucADCValBuf[ucADCCount]<ucMin)
			{
				ucMin = ucADCValBuf[ucADCCount];
			}

			if(ucADCValBuf[ucADCCount]>ucMax)
			{
				ucMax = ucADCValBuf[ucADCCount];
			}                        
		}

		ucADCValSum -= ucMax;
		ucADCValSum -= ucMin;

		ucMax = ucADCValSum>>2;	//adc val
		
		ucMin = 4;
		if (enState == EN_STATE_IDLE)
		{
			ucMin = 0;
		}
		if (enState == EN_STATE_OUTPUT)
		{
			ucMin = 8;
		}
		
		for(ucADCCount=0; ucADCCount<4; ucADCCount++)
		{
			if (ucMax>=ADCTable[ucMin+ucADCCount])
			{
				break;
			}
		}

		ucMax = 0;
		if (ucADCCount<4)
		{
			ucMax = ADCTable2[ucMin+ucADCCount];
		}

/*
		if (gbChargeOK)
		{
			ucPowerPercent = 5;
		}
		else
*/
		if (gbFristPowerOn)
		{
			ucPowerPercent = ucMax;
		}
		else
		{
			if (enState == EN_STATE_CHARGE)
			{
				if(ucMax>=ucPowerPercent)
					ucPowerPercent = ucMax;	
				//if (ucPowerPercent==10)
					//gbChargeOK = TRUE;
			}					
			if(enState == EN_STATE_OUTPUT)
			{
				if(ucMax<=ucPowerPercent)
					ucPowerPercent = ucMax;
			}

			//ucPowerPercent = ucMax;
		}

		ucADCCount = 0;
	}

	FADENB = 0;
	ADM = 0x02;	//ADC input channel mode: AIN2
	FGCHS = 1;	//AIN channel enable
	FADENB = 1;
	//ADR = 0x00;	//ADC clock: 1.000000MHZ,ADC resolution: -1
	//VREFH = 0x00;	//Internal VREFH: NULL
	FEVHENB = 1;	//Internal VREFH disable
	FADENB = 1;
	FADCIRQ = 0;
	FADCIEN = 1;	//Interrupt enable
	FEOC = 0;
	//FADS = 1;					
	
	gucADCSel = 1;
	g5MsTimeoutCount = 0;
}

